<?php
class Controller_Admin_Member extends Controller_Template
{

	private $_limit;
	public function __construct($request, $response)
	{
		parent::__construct($request, $response);
	
		$this->_limit	= Kohana::config('member.page_size');
		$this->template = 'smarty:admin/member/index';
		$this->_domain	= Site::domain('member');
	}

	public function action_index()
	{
		$this->action_search();
	}
	
	public function action_search()
	{
		if(strval($this->request->query('login_name')))
			$passport	= strval($this->request->query('login_name'));
		
		if(strpos($passport, '@') > 0)
			$condition['passport']	= $passport;
		elseif($passport && $passport != '填写姓名或者账号')
			$condition['nickname']	= $passport;
		
		$begin_time	= $this->request->query('begin_time');
		$end_time	= $this->request->query('end_time');

		if(!empty($begin_time))
			$condition['create_time_greater']	= strtotime($begin_time);
		if(!empty($end_time))
			$condition['create_time_less']		= strtotime($end_time);
		$condition['order_by']	= array('create_time' => 'desc');	
		$condition['site']		= 'merchant';

		$actived		= Kohana::config('user.activated');
		$unactived		= Kohana::config('user.unactivated');
	
		if(intval($this->request->query('status')))
			$condition['status'] = intval($this->request->query('status')) == 1 ? 'activated': 'unactivated';
		
		$page			= isset($_GET['page']) ? intval($_GET['page']) : 1;

		$offset			= $this->_limit * ($page - 1);
	
		$rs				= User::search($condition, $offset, $this->_limit);
		
		$count			= $rs['count'];

		$page_config	= array(
							'total_items'		=> $count,
							'items_per_page'	=> $this->_limit,
				);
		$data			= $rs['list'];	
		for($i=0 ; $i < count($data); $i++)
		{
			$data[$i]['create_time']	= date('Y-m-d H:i:s' , $data[$i]['create_time']);

			$data[$i]['status_name']	= intval($data[$i]['status'] & 1) ? '已激活' : '未激活';
		}

		$pagination		= Pagination::factory($page_config);
		
		$this->template->set_filename('smarty:admin/member/list');
		$this->template->set('total', $count);
		$this->template->set('data', $data);
		$this->template->set('login_name', $passport);
		$this->template->set('status', $this->request->query('status'));
		$this->template->set('start_time', $begin_time);
		$this->template->set('end_time', $end_time);
		$this->template->set('page_title', '渠道商列表');
		$this->template->set('pagination', $pagination->render());
		$function_name	= Request::$current->controller();
		$this->template->set('function_name', $function_name);
		$this->response->body($this->template->render());
	}

	public function action_del_member()
	{
		if(isset($_POST['action']) && $_POST['action'] == 'del')
		{
			$page = 1;
			$arr_del = array();
			foreach($_POST as $key => $value)
			{
				if(strpos($key,'u_') === 0)
				{
					$key = explode('_',$key);
					$arr_del[] = $key[1];
				}
			}
			//print_r($arr_del);
			//删除会员
		}
		//成功提示信息
		$url = '/member/';
		$this->request->redirect($url, $code = 302);
	}

	public function action_detail($uid)
	{
	
		$uid	= intval($uid);

		if($uid <= 0)
			return false;
		
		$query['uid']	= $uid;
		
		$users_info = User::search($query);

		$data		= empty($users_info) ? null : $users_info['list'][0];

		$page		= isset($_GET['page']) ? intval($_GET['page']) : 1;

		$limit		= 20;

		$offset		= $limit * ($page - 1);

		if(isset($data['uid']))
			$data['im'] = User::im($data['uid']);

		if(empty($data['realname']))
			$data['realname']	= $data['nickname'];
		
		if(!empty($data['create_time']))
			$data['create_time']	= date('Y-m-d H:i:s', $data['create_time']);
		
		$condition['member']	= $uid;
		$condition['include_recv_address']	= true;
		
		$orders_info = Order::search($condition, $offset, $limit);
		
		$count		 = empty($orders_info) ? 0 : $orders_info['count'];

		$page_config = array(
							'total_items'		=> $count,
							'items_per_page'	=> $limit,
						);

		$address_list = array();
		$pagination	 = Pagination::factory($page_config);
		$order_info	 = '';
		$order_info	 = empty($orders_info) ? null : $orders_info['list'];
		
		if(count($order_info) > 0)
		{
			foreach($order_info as $key => $val)
			{
				if(!empty($val['pay_time']))
					$order_info[$key]['pay_time']    = date('Y-m-d H:i:s', $val['pay_time']);

				if(!empty($val['create_time']))
					$order_info[$key]['create_time'] = date('Y-m-d H:i:s', $val['create_time']);

				$order_info[$key]['shop_name']       = Shop::name($order_info[$key]['shop_id']);
				$order_info[$key]['shop_url']        = Shop::shop_detail_url($order_info[$key]['shop_id']);

				foreach($order_info[$key]['goods'] as $k => $v)
				{
					$tmp['title']    = $v['title'];
					$tmp['ec_title'] = urlencode($v['title']);
					$tmp['goods_id'] = $v['goods_id'];
					$tmp['g_url']    = Goods::goods_detail_url($v['goods_id']);
					$order_info[$key]['goods_info'][]	= $tmp;
				}

				$address_list[] = $val['recv_addr']['province'].' '.$val['recv_addr']['city'].' '.$val['recv_addr']['address'].' '.$val['recv_addr']['username'].' '.$val['recv_addr']['mobile'].'<br>';
			}
		}
		$address_list = array_unique($address_list);

		$shop_info = '';
		$utype	   = 'member';
		if(!empty($data))
		{
			$utype	= $data['utype'];
			if($data['utype'] == 'merchant')
			{
				//商家信息
				$obj_shop	= new Model_Shopinfo();
				$shops_info	= $obj_shop->get_shopinfo_byuid($data['uid']);
				if(!empty($shops_info))
				{
					$shop_id	= $shops_info[0]['id'];
					
					$shop_info	= Shop::shop_info($shop_id);

					if(!empty($shop_info) && count($shop_info) > 0)
					{
						$this->_upimage  = new Model_Upimage();
				 
						$license		 =	$this->_upimage->get_image_by_target(Kohana::config('upimage.target_type.business_licence'),$shop_id); 
						
						$brand			 = $this->_upimage->get_image_by_target(Kohana::config('upimage.target_type.brand_licence'),$shop_id);
						
						$logo		     = $this->_upimage->get_image_by_target(Kohana::config('upimage.target_type.shop_logo'),$shop_id);
						
						$shop_info['license']		= empty($license) ? null : $license['0'];
						$shop_info['brand']			= empty($brand)	  ? null : $brand;
						$shop_info['logo']			= empty($logo)	  ? null : $logo[0];

						$name = & Category_System::name($shop_info['major_category_id']);
						Category_system::commit_names();
						
						$shop_info['major_category']= empty($shop_info) ? null : $name;
						$shop_info['apply_time']	= date('Y-m-d H:i:s', $shop_info['apply_time']);
						$shop_info['province_name']	= City::province_name($shop_info['province']);
						$shop_info['city_name']		= City::city_name($shop_info['city']);		
					}

				}
			}
		
		}

		$url	= 'http://'.$this->_domain.'/shop/goods';
		$this->template->set_filename('smarty:admin/member/detail');
		$this->template->set('info', $data);
		$this->template->set('url', $url);
		$this->template->set('order_info', $order_info);
		$this->template->set('address_list', $address_list);
		$this->template->set('data', $shop_info);
		$this->template->set('pagination', $pagination->render());
		$this->template->set('utype', $utype);
		$this->template->set('total', $count);
		$this->template->set('uid',$uid);
		$this->template->set('page_title', '会员信息详情');
		$this->response->body($this->template->render());
	}

	/**
	 * @param int $uid 统计此member动作列表 
	 *
	 * @return 多种动作列表合并，按时间排序
	 *	返回数组中公共存在3个元素 'url','create_time','type';
	 *	其它值详见获取动作列表方法注释。
	 */
	protected function get_action_list($uid)
	{
		$action_list  = array();
		$fav_list     = $this->favorite_list($uid);
		$message_list = $this->message_list($uid);
		$list         = array_merge($fav_list,$message_list);
		$action_list  = $this->sort_array($list, 'create_time'); 

		return $action_list;
	}
	
	/**
	 * 获取某用户收藏动作列表
	 *
	 * @param int $uid 用户id
	 *
	 * @return array(
	 *  array(
		 *  'create_time' => '2011-11-11 11:11:11',
		 *  'url'         => 'http://jia.focus.cn/shop/index/44', //产生动作的url
		 *  'type'        => 'goods_fav', //动作类型
		 *  'name'        => '描述信息', //动作操作对象名称或描述
	 *  ),
	 *  array(
		 *  'create_time' => '2011-11-11 11:11:11',
		 *  'url'         => 'http://jia.focus.cn/shop/index/44', //产生动作的url
		 *  'type'        => 'goods_fav', //动作类型
		 *  'name'        => '描述信息', //动作操作对象名称或描述
	 *  )
	 * ) 
	 */
	protected function favorite_list($uid)
	{
		$fav_list        = array();
		$myfavorite      = new Model_My_Myfavorite();
		$fav_goods_list  = $myfavorite->get_list_uid($uid,Kohana::config('myfavorite_config.status.goods_favorite'));
		$fav_shop_list   = $myfavorite->get_list_uid($uid,Kohana::config('myfavorite_config.status.shop_favorite'));
		$favs        = array_merge($fav_goods_list,$fav_shop_list);
		if(!empty($favs))
		{
			foreach($favs as $f)
			{
				$fav['create_time'] = $f['create_time'];
				$fav['url']         = $f['url'];
				if($f['type'] == Kohana::config('myfavorite_config.status.goods_favorite'))
				{
					$fav['type'] = 'goods_fav';
					$fav['name'] = $f['title'];
				}
				else
				{
					$fav['type'] = 'shop_fav';
					$fav['name'] = $f['shop_name'];
				}
				$fav_list[] = $fav;
			}
		}

		return $fav_list;
	}

	/**
	 * 获取某用户留言动作列表
	 *
	 * @param int $uid 用户id
	 *
	 * @return array(
	 *  array(
		 *  'create_time' => '2011-11-11 11:11:11',
		 *  'url'         => 'http://jia.focus.cn/shop/index/44', //产生动作的url
		 *  'type'        => 'goods_fav', //动作类型
		 *  'name_msg'    => '留言', //留言内容
		 *  'name_rep'    => '回复', //回复内容
	 *  ),
	 *  array(
		 *  'create_time' => '2011-11-11 11:11:11',
		 *  'url'         => 'http://jia.focus.cn/shop/index/44', //产生动作的url
		 *  'type'        => 'goods_fav', //动作类型
		 *  'name_msg'    => '留言', //留言内容
		 *  'name_rep'    => '回复', //回复内容
	 *  )
	 * ) 
	 */
	protected function message_list($uid)
	{
		$message		= new Model_Message();
		$message_rs     = array();
		$rs             = $message->getUserMessages($uid,0,10000);

		if(!empty($rs))
		{
			foreach($rs as $ms)
			{
				$m['create_time'] = date('Y-m-d H:i:s',$ms['create_time']);
				if($ms['target_type'] == Kohana::config('message.type.goods'))
				{
					$m['url']         = Goods::goods_detail_url($ms['target_id']);
					$m['type']       = 'goods_msg';
				}
				if($ms['target_type'] == Kohana::config('message.type.shop'))
				{
					$m['url']    = Shop::shop_detail_url($ms['target_id']);
					$m['type']       = 'shop_msg';
				}
				$m['name_msg']   = $ms['content'];
				$m['name_rep']   = isset($ms['reply_content'][0])?$ms['reply_content'][0]['content']:'';

				$message_rs[] = $m;
			}
		}

		return $message_rs;
	}

	/**
	 * 多维数组按某列排序
	 * @param $arr = array(
	 *  array('id'=>1, 'create_time'=>'2012-12-12 12:12:12', 'sort'=>1),
	 *  array('id'=>2, 'create_time'=>'2012-12-12 12:12:13', 'sort'=>4),
	 *  array('id'=>3, 'create_time'=>'2012-12-12 12:12:11', 'sort'=>3),
	 *  array('id'=>4, 'create_time'=>'2012-12-12 12:12:10', 'sort'=>2),
	 * )
	 * @param $col 排序列的名
	 * @param $order 排序
	 *
	 * @return $new_arr 上边的数据原数据排序后返回，保持关联
	 *  ex: $col = create_time $order = 'asc';
	 * $new_arr = array(
	 *  array('id'=>4, 'create_time'=>'2012-12-12 12:12:10', 'sort'=>2),
	 *  array('id'=>3, 'create_time'=>'2012-12-12 12:12:11', 'sort'=>3),
	 *  array('id'=>1, 'create_time'=>'2012-12-12 12:12:12', 'sort'=>1),
	 *  array('id'=>2, 'create_time'=>'2012-12-12 12:12:13', 'sort'=>4),
	 * )
	 */
	public static function sort_array(& $arr, $col, $order = 'ASC')
	{
		$new_arr = array();
		if(empty($arr))
			return $new_array;

		foreach($arr as $key=>$value)
		{
			$temp[$key]= $value[$col];
		}

		if($order=="ASC")
		{
			 asort($temp,SORT_STRING);//升序排序临时组成的数组
		}

		if($order=="DESC")
		{
			asort($temp,SORT_STRING);
			arsort($temp,SORT_STRING);//反转
		}

		foreach($temp as $temp_key=>$temp_value)
		{
			$new_arr[]= $arr[$temp_key];//读出数据
		}

		return $new_arr;
	}
}

?>
